package com.longge;

import java.util.HashMap;
import java.util.Map;

public class MaxSubarrayLength {

    public int maxSubarrayLength(int[] nums, int k) {
        Map<Integer, Integer> freqDict = new HashMap<>();
        int maxLength = 0;
        int left = 0;
        for (int right = 0; right < nums.length; right++) {
            freqDict.put(nums[right], freqDict.getOrDefault(nums[right], 0) + 1);
            while (freqDict.get(nums[right])>k) {
                freqDict.put(nums[left], freqDict.get(nums[left]) - 1);
                left++;
            }
            maxLength = Math.max(maxLength, right - left + 1);
        }
        return maxLength;
    }


}
